#include "Camera.h"

Camera::Camera(vec3 position, vec3 target, vec3 worldup) {

	Position = position;
	worldUp = worldup;
	Forward = normalize(target - position);
	Right = normalize(cross(Forward, worldUp));
	Up = normalize(cross(Forward, Right));
}
Camera::Camera(vec3 position, float pitch, float yaw, vec3 worldup) {
	Position = position;
	worldUp = worldup;
	Pitch = pitch;
	Yaw = yaw;
	Forward.x = cos(Pitch) * sin(Yaw +radians(180.0f));
	Forward.y = sin(Pitch);
	Forward.z = cos(Pitch) * cos(Yaw + radians(180.0f));
	Right = normalize(cross(Forward, worldUp));
	Up = normalize(cross(Forward, Right));
}
mat4 Camera::GetViewMatrix() {
	return lookAt(Position, Position + Forward, worldUp);
}

void Camera::UpdateCameraVectors() {
	Forward.x = cos(Pitch) * sin(Yaw + radians(180.0f));
	Forward.y = sin(Pitch);
	Forward.z = cos(Pitch) * cos(Yaw + radians(180.0f));
	Right = normalize(cross(Forward, worldUp));
	Up = normalize(cross(Forward, Right));
}

void Camera::ProcessMouseMovement(float deltaX, float deltaY) {
	Pitch += deltaY;
	Yaw += deltaX;
	if (Pitch > radians(89.0f))Pitch = radians(89.0f);
	if (Pitch < radians(-89.0f))Pitch = radians(-89.0f);
	UpdateCameraVectors();
}